<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <input type="text" id="inputId">
    <button onclick="cal()">阶乘</button>
    <span id="resultId"></span>

    <script>
        function cal() {
            var num = document.getElementById('inputId').value;
            var resultObj = document.getElementById('resultId');
            if (num == ''){
                resultObj.innerText = '不能为空';
                return;
            }
            if (isNaN(num)) {
                resultObj.innerText = '请输入一个数字';
                return;
            }

            // n!=n*(n-1)*(n-2)...*2*1
            var result = 1;
            for (var i = num; i >= 1; i--) {
                result = result * i;
            }
            resultObj.innerText = result;
        }
    </script>
</body>
</html>